var express = require("express");
var router = express.Router();
const getTime = require("../../utils/getTime");
const adModel = require("../../models/adModel");

// 获取
router.get("/ad/info", (req, res) => {
  let { pageNo, pageSize } = req.query;
  if (pageNo === "undefined" || pageSize === "undefined") {
    adModel
      .find()
      .select({ _v: 0 })
      .sort({ time: -1 })
      .then((data) => {
        if (data.length === 0) {
          return res.json({
            code: 200,
            msg: "数据为空",
            data: data,
          });
        }
        return res.json({
          code: 200,
          msg: "获取信息成功",
          data: data,
        });
      })
      .catch((err) => {
        return res.json({
          code: "1001",
          msg: "数据库读取失败~~~",
          data: null,
        });
      });
  } else {
    adModel
      .find()
      .then((result) => {
        if (result.length === 0) {
          return res.json({
            code: 200,
            msg: "政策数据为空",
            data: null,
          });
        } else {
          adModel
            .find()
            .select({ _v: 0 })
            .sort({ time: -1 })
            .skip((pageNo - 1) * pageSize)
            .limit(pageSize)
            .then((data) => {
              let total = result.length;
              return res.json({
                code: 200,
                msg: "获取信息成功",
                data: data,
                total: total,
              });
            })
            .catch((err) => {
              return res.json({
                code: "1001",
                msg: "数据库读取失败~~~",
                data: null,
              });
            });
        }
      })
      .catch((err) => {
        return res.json({
          code: "1001",
          msg: "数据库读取失败~",
          data: null,
        });
      });
  }
});

// 广告上下架
router.post("/ad/handle", (req, res) => {
  let { _id, is_hot } = req.body;
  let time = getTime(new Date());
  adModel
    .updateOne({ _id }, { is_hot, time })
    .then((data) => {
      if (!data.acknowledged) {
        return res.json({
          code: -1,
          msg: "没有数据",
          data: null,
        });
      }
      return res.json({
        code: 200,
        msg: "操作成功~",
        data: null,
      });
    })
    .catch((err) => {
      return res.json({
        code: "1001",
        msg: "数据库读取失败~~~",
        data: null,
      });
    });
});

// 新增
router.post("/ad/add", (req, res) => {
  let time = getTime(new Date());
  let { imgUrl } = req.body;
  adModel
    .create({ imgUrl, is_hot: false, time })
    .then((data) => {
      if (!data) {
        return res.json({
          code: -1,
          msg: "新增失败",
          data: null,
        });
      }
      return res.json({
        code: 200,
        msg: "新增成功",
        data: data,
      });
    })
    .catch((err) => {
      return res.json({
        code: "1001",
        msg: "数据库读取失败~~~",
        data: null,
      });
    });
});

// 修改
router.post("/ad/update", (req, res) => {
  let { _id, imgUrl, is_hot } = req.body;
  let time = getTime(new Date());
  adModel
    .updateOne({ _id }, { imgUrl, is_hot, time })
    .then((data) => {
      if (!data.acknowledged) {
        return res.json({
          code: -1,
          msg: "没有数据",
          data: null,
        });
      }
      return res.json({
        code: 200,
        msg: "修改成功~",
        data: null,
      });
    })
    .catch((err) => {
      return res.json({
        code: "1001",
        msg: "数据库读取失败~~~",
        data: null,
      });
    });
});

// 删除单条
router.post("/ad/remove", (req, res) => {
  let { _id } = req.body;
  adModel
    .deleteOne({ _id })
    .then((result) => {
      if (result.deletedCount !== 0) {
        return res.json({
          code: 200,
          msg: "删除成功",
          data: null,
        });
      }
      return res.json({
        code: -1,
        msg: "删除失败",
        data: null,
      });
    })
    .catch((err) => {
      return res.json({
        code: "1001",
        msg: "数据库读取失败~",
        data: null,
      });
    });
});

// 批量删除
router.post("/ad/delBatch", (req, res) => {
  adModel
    .deleteMany({ _id: { $in: req.body } })
    .then((result) => {
      if (result.deletedCount !== 0) {
        return res.json({
          code: 200,
          msg: "删除成功",
          data: null,
        });
      }
      return res.json({
        code: -1,
        msg: "删除失败",
        data: null,
      });
    })
    .catch((err) => {
      return res.json({
        code: "1001",
        msg: "数据库读取失败~",
        data: null,
      });
    });
});

module.exports = router;
